package com.cdshangpai.photoapp.ui.hotal;

import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.widget.TextView;

import androidx.databinding.DataBindingUtil;

import com.alibaba.fastjson.JSONObject;
import com.cdshangpai.photoapp.PhotoAppBaseActivity;
import com.cdshangpai.photoapp.R;
import com.cdshangpai.photoapp.databinding.ActivityHotalListBinding;
import com.cdshangpai.photoapp.ui.OnListViewClickListener;
import com.cdshangpai.photoapp.ui.ViewMarginAdjust;
import com.cdshangpai.utils.db.HotalDb;
import com.cdshangpai.utils.db.beans.Hotal;
import com.cdshangpai.utils.system.Keywboard;
import com.google.android.flexbox.AlignItems;
import com.google.android.flexbox.FlexDirection;
import com.google.android.flexbox.FlexWrap;
import com.google.android.flexbox.FlexboxLayoutManager;
import com.google.android.flexbox.JustifyContent;

import java.util.ArrayList;
import java.util.List;

public class HotalListActivity extends PhotoAppBaseActivity implements OnListViewClickListener {
    ActivityHotalListBinding binding;
    HotalDb hotalDb;
    SearchFilter searchFilter=new SearchFilter();

    @Override
    public void onClick(Integer position) {
        Hotal hotal=binding.getAdapter().datas.get(position);
        String param= JSONObject.toJSONString(hotal);

        Intent intent=new Intent(this,HotalDetailActivity.class);
        intent.putExtra("param",param);
        startActivity(intent);
    }


    public class SearchFilter{
        ArrayList<String> citys=new ArrayList<>();
        String keyword="";
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding= DataBindingUtil.setContentView(this,R.layout.activity_hotal_list);
        binding.hotalList.setOverScrollMode(View.OVER_SCROLL_NEVER);
        binding.setNonedata(false);

        if(hotalDb==null)
        {
            hotalDb=new HotalDb();
        }

        binding.setView(this);

        HotalListAdapter adapter=new HotalListAdapter(this,this);
        binding.setAdapter(adapter);

        FlexboxLayoutManager layoutManager=getFlexboxLayoutManager(false);
        binding.hotalList.setLayoutManager(layoutManager);


        search();

        binding.hotalListKeyword.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                if(actionId== EditorInfo.IME_ACTION_SEARCH)
                {
                    List<View> views=new ArrayList<>();
                    views.add(binding.hotalListKeyword);
                    Keywboard.hideSoftKeyboard(getApplicationContext(),views);

                    onSearch();
                    return true;
                }
                else {
                    return false;
                }
            }
        });
    }

    @Override
    protected void onResume() {
        super.onResume();
        ViewMarginAdjust.adjustListPosition(binding.hotalListBody);
    }

    private FlexboxLayoutManager getFlexboxLayoutManager(Boolean isCenter)
    {
        FlexboxLayoutManager layoutManager = new FlexboxLayoutManager(this);
        layoutManager.setFlexWrap(FlexWrap.WRAP);
        layoutManager.setFlexDirection(FlexDirection.ROW);
        if(isCenter)
        {
            layoutManager.setJustifyContent(JustifyContent.CENTER);
            layoutManager.setAlignItems(AlignItems.CENTER);
        }
        else {
            layoutManager.setAlignItems(AlignItems.STRETCH);
            layoutManager.setJustifyContent(JustifyContent.FLEX_START);
        }
        return layoutManager;
    }

    public void onSearch()
    {
        List<View> views=new ArrayList<>();
        views.add(binding.hotalListKeyword);
        Keywboard.hideSoftKeyboard(this,views);

        searchFilter.keyword=binding.hotalListKeyword.getText().toString();
        search();
    }

    private void search()
    {
        List<Hotal> hotals=hotalDb.query(searchFilter.citys.toArray(new String[searchFilter.citys.size()]),searchFilter.keyword);
        binding.getAdapter().datas=hotals;
        binding.getAdapter().notifyDataSetChanged();
        binding.setNonedata(hotals.size()==0);
    }

    public void selectMenu(View v, String city)
    {
        if(!cityChecked(city))
        {
            searchFilter.citys.add(city);
        }
        else
        {
            searchFilter.citys.remove(city);
        }
        binding.setView(this);
        search();
    }

    public void gotoHome(View v)
    {
        this.finish();
    }


    public Boolean cityChecked(String city)
    {
        for(String item:searchFilter.citys)
        {
            if(city.equals(item))
            {
                return true;
            }
        }
        return false;
    }

}